//
//  HTCFSystemUitil.m
//  HTHappySale
//
//  Created by 创维海通 on 2017/9/14.
//  Copyright © 2017年 zxyywy. All rights reserved.
//

#import "HTCFSystemUitil.h"
#import "sys/utsname.h"
#import <CommonCrypto/CommonDigest.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <AVFoundation/AVFoundation.h>

@implementation HTCFSystemUitil

#pragma mark - 获取设备的前后摄像头像素
+ (NSString *)getResolutionOfFront:(BOOL)isFront andBehind:(BOOL)isBehind
{
    struct utsname systemInfo;
    uname(&systemInfo);
    
    NSString *machine =[[NSString alloc] initWithString:[NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]];
    
    /*
     设备名：后置摄像头：前置摄像头
     iPhone 4:500W:30W
     iPhone 4S：800W：30W
     iPhone 5：800W：120W
     iPhone 5C：800W：120W
     iPhone 5S：800W：120W
     iPhone 6：800W：120W
     iPhone 6Plus:800W：120W
     iPhone 6s :1200W: 500W
     iPhone 6s Plus:1200W: 500W
     */
    
    machine = [[machine componentsSeparatedByString:@","] objectAtIndex:0];
    
    NSArray *iPhoneArr = [[NSArray alloc] initWithObjects:
                          @"iPhone4:500W:30W",
                          @"iPhone4S：800W：30W",
                          @"iPhone5：800W：120W",
                          @"iPhone5C：800W：120W",
                          @"iPhone5S：800W：120W",
                          @"iPhone6：800W：120W",
                          @"iPhone6Plus：800W：120W",
                          @"iPhone6s：1200W：500W",
                          @"iPhone6s Plus：1200W：500W",
                          
                          
                          @"iPad1:无摄像头:无摄像头",
                          @"iPad2:30W:70W",
                          @"iPad4:120W:500W",
                          @"The New iPad:30W:500W",
                          @"iPad mini:120W:500W",
                          @"iPad mini2:120W:500W",
                          @"iPad mini3:120W:500W",
                          @"iPad Air:120W:500W",
                          @"iPad Air2:120W:800W",nil];
    /*
     设备名：前置摄像头：后置摄像头
     iPad 1:无摄像头:无摄像头
     iPad 2:30W:70W
     iPad 4:120W:500W
     
     The New iPad:30W:500W
     iPad mini:120W:500W
     iPad mini 2:120W:500W
     iPad mini 3:120W:500W
     
     iPad Air:120W:500W
     iPad Air 2:120W:800W
     
     */
    NSArray *iPadArr = [[NSArray alloc] initWithObjects:
                        @"iPad1:无摄像头:无摄像头",
                        @"iPad2:30W:70W",
                        @"iPad4:120W:500W",
                        @"The New iPad:30W:500W",
                        @"iPad mini:120W:500W",
                        @"iPad mini2:120W:500W",
                        @"iPad mini3:120W:500W",
                        @"iPad Air:120W:500W",
                        @"iPad Air2:120W:800W", nil];
    
    NSString *roStr = @"";
    
    if (isFront) {
        for (NSString *str in iPhoneArr) {
            if ([machine isEqualToString:[[str componentsSeparatedByString:@":"] objectAtIndex:0]]) {
                roStr = [[str componentsSeparatedByString:@":"] objectAtIndex:2];
            }
        }
        
    }else{
        for (NSString *str in iPhoneArr) {
            if ([machine isEqualToString:[[str componentsSeparatedByString:@":"] objectAtIndex:0]]) {
                roStr = [[str componentsSeparatedByString:@":"] objectAtIndex:1];
            }
        }
        
    }
    return roStr;
}

#pragma mark获取 设备id号
+ (NSString *)Manufacturers_ID
{
    //CFUUID
    CFUUIDRef cfuuid = CFUUIDCreate(kCFAllocatorDefault);
    NSString *cfuuidString = (NSString*)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, cfuuid));
    NSLog(@"CFUUID:%@", cfuuidString);
    
    
    //NSUUID
    NSString *uuid = [[NSUUID UUID] UUIDString];
    NSLog(@"NSUDID:%@", uuid);
    
    return cfuuidString;
}

#pragma mark获取 设备UUID号
+ (NSString *) uuid
{
    CFUUIDRef puuid = CFUUIDCreate( nil );
    CFStringRef uuidString = CFUUIDCreateString( nil, puuid );
    NSString * result = (NSString *)CFBridgingRelease(CFStringCreateCopy( NULL, uuidString));
    CFRelease(puuid);
    CFRelease(uuidString);
    return result ;
}

#pragma mark获取 设备IDFV号
+ (NSString *) idfv
{
    NSString *idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    NSLog(@"idfv:%@", idfv);
    return idfv;
}

#pragma mark获取 RAM容量
+ (NSString *)getRAM_Capacity
{
    NSProcessInfo *info = [[NSProcessInfo alloc]init];
    return [NSString stringWithFormat:@"%lluMB",[info physicalMemory]/1024/1024];
}
#pragma mark 获取  设备总容量
+ (NSString *)totalDiskSpace
{
    NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
    
    long long totalSpace = [[fattributes objectForKey:NSFileSystemSize] longLongValue];
    NSString *spaceStr = [NSString stringWithFormat:@"%lldMB",totalSpace/1024/1024];
    
    return spaceStr;
}
#pragma mark 获取  设备可用容量
+ (NSString *)freeDiskSpace
{
    NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
    
    long long totalSpace = [[fattributes objectForKey:NSFileSystemFreeSize] longLongValue];
    NSString *spaceStr = [NSString stringWithFormat:@"%lldMB",totalSpace/1024/1024];
    
    return spaceStr;
}

+ (CGFloat)appVersion
{
    NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
    return [infoDict[@"CFBundleShortVersionString"] floatValue];
}


+ (CGFloat)appFloatVersion
{
    NSDictionary *infoDict = [[NSBundle mainBundle]infoDictionary];
    return [infoDict[@"CFBundleShortVersionString"] floatValue];
}

+ (NSString *)appStringVersion
{
    NSDictionary *infoDict = [[NSBundle mainBundle]infoDictionary];
    return infoDict[@"CFBundleShortVersionString"];
}

+ (NSString *)appBundleIdentifier
{
    return [[NSBundle mainBundle]bundleIdentifier];
}

+ (CGRect)screenBounds{
    return [UIScreen mainScreen].bounds;
}

+ (CGFloat)currentSystemVersion{
    return [[UIDevice currentDevice].systemVersion floatValue];
}

+ (CGFloat)currentScreenScale{
    return [UIScreen mainScreen].scale;
}

+ (BOOL)isSystemVersionOver:(CGFloat)versionValue
{
    return [HTCFSystemUitil currentSystemVersion] >= versionValue;
}

+ (CGSize)deviceScreenSize{
    return [UIScreen mainScreen].bounds.size;
}

+ (BOOL)iPhone4Device{
    return CGSizeEqualToSize((CGSize){320,480}, [HTCFSystemUitil deviceScreenSize]);
}

+ (BOOL)iPhone5Device{
    return CGSizeEqualToSize((CGSize){320,568}, [HTCFSystemUitil deviceScreenSize]);
}

+ (BOOL)iPhone6Device{
    return CGSizeEqualToSize((CGSize){375,667}, [HTCFSystemUitil deviceScreenSize]);
}

+ (BOOL)iPhone6PlusDevice{
    return CGSizeEqualToSize((CGSize){414,736}, [HTCFSystemUitil deviceScreenSize]);
}

+ (BOOL)iPadDevice{
    return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
}

+ (CGFloat)naivationBarHeight
{
    return 44.f;
}

+ (NSString *)mainBundlePath:(NSString*)fileName{
    if (HTCFStringIsNull(fileName)) {
        return nil;
    }
    NSArray *fileArray = [fileName componentsSeparatedByString:@"."];
    if (fileArray.count < 2) {
        return nil;
    }
    return [HTCFMainBundle  pathForResource:fileArray[0] ofType:fileArray[1]];
}

+ (NSString *)bundle:(NSString *)bundle file:(NSString *)file{
    if (HTCFStringIsNull(bundle)||HTCFStringIsNull(file)) {
        return nil;
    }
    return [bundle stringByAppendingPathComponent:file];
}

+ (BOOL)isAppPhotoLibraryAccessAuthorized
{
    ALAuthorizationStatus authStatus = [ALAssetsLibrary authorizationStatus];
    
    if (authStatus != ALAuthorizationStatusAuthorized) {
        
        return authStatus == ALAuthorizationStatusNotDetermined;
        
    }else{
        
        return YES;
    }
}

+ (BOOL)isAppCameraAccessAuthorized
{
    /* iOS7以上才有相机隐私控制 */
    if (!HTCFSystemIsOver7) {
        return YES;
    }
    
    NSString *mediaType = AVMediaTypeVideo;
    
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
    
    if (authStatus !=  AVAuthorizationStatusAuthorized) {
        
        return authStatus == AVAuthorizationStatusNotDetermined;
        
    }else{
        
        return YES;
    }
}

+ (NSNotificationCenter *)defaultCenter
{
    return [NSNotificationCenter defaultCenter];
}

+ (void)postNoti:(NSString *)notiName
{
    [HTCFSystemUitil postNoti:notiName withObject:nil];
}

+ (void)postNoti:(NSString *)notiName withObject:(id)obj
{
    [HTCFSystemUitil postNoti:notiName withObject:obj withUserInfo:nil];
}

+ (void)postNoti:(NSString *)notiName withObject:(id)obj withUserInfo:(NSDictionary *)infoDict
{
    if (HTCFStringIsNull(notiName)) {
        return;
    }
    [[HTCFSystemUitil defaultCenter] postNotificationName:notiName object:obj userInfo:infoDict];
}

+ (CGFloat)widthScale{
    return [self deviceScreenSize].width / 320;
}

+ (CGFloat)heightScale{
    return [self deviceScreenSize].width / 568;
}

@end
